

import { defineStore } from 'pinia'
import { ref,watch } from 'vue'
// import { useRouter,useRoute } from 'vue-router';
function initState() {
   return {
        isCollapse: false,
        // 导航值
        tags:[
          {
            path:'/home',
            name:'home',
            label:'首页',
            icon:'home'
          }
        ],
        currentMenu:null,
        // 菜单
        menuList:[],
        // token
        token:"",
        routerList:[]
   }
}
export const useAllDataStore = defineStore(
    'allData', () => {
      const state = ref(initState())
      //这里来解决持久化的问题
      watch(state,(newObj)=>{ 
        if(!newObj.token) return
        localStorage.setItem('store',JSON.stringify(newObj))
      },{deep:true})
      // 这里来处理tag标签
      function selectMenu(val) {
        if(val.name === 'home'){
          state.value.currentMenu = null
        } else {
          state.value.currentMenu = val
          // 这里来判断tag标签的重复添加
         let index = state.value.tags.findIndex(item => item.name === val.name)
          index === -1 ? state.value.tags.push(val) : ''
        }
      }
      // 删除1111
      function undateTags(tag){
        let index = state.value.tags.findIndex(item => item.name === tag.name)
        state.value.tags.splice(index,1)
      }
      // 更新路由
      function updateMenuList(val){
        state.value.menuList = val
      }
      // 动态路由方法
      function addMenu(router,type){
        if(type === 'refresh') {
          if(JSON.parse(localStorage.getItem('store'))){
            state.value = JSON.parse(localStorage.getItem('store'))
            // 
            state.value.routerList = []
          } else {
            return
          }
        }
        // console.log('router',router);
        
        // 这里是菜单
        const menu = state.value.menuList     
        // glob导入，vite支持使用特殊的import.meta.glob 函数从文件系统导入多个模块
        // 这里会遇见路径问题，import.meta.glob("../../views/**/*.vue")
        // https://cn.vitejs.dev/guide/features#glob-import
        // const module = import.meta.glob("../views/**/*.vue")
        const module = import.meta.glob("../../views/**/*.vue")
        // console.log('module',module);
        const routeArr= []
        menu.forEach(item => {
          if(item.children){
            item.children.forEach(val => {
              let url=`../../views/${val.url}.vue`
              val.component = module[url]
              routeArr.push(...item.children)
            })
          } else {
            let url = `../../views/${item.url}.vue`
            // console.log('module[url]',url,'===========',module);
            item.component = module[url]
            routeArr.push(item)

          }
        })
        state.value.routerList = []
        // console.log('router',router.getRoutes());
        let routers = router.getRoutes()
        routers.forEach(item => {
          if(item.name == 'main' || item.name == 'login' || item.name == '404'){
            return
          } else {
            router.removeRoute(item.name)
          }
        })
        routeArr.forEach(item => {
          state.value.routerList.push(router.addRoute("main",item))
        })
      }
      function clean(){
        // 清除路由
        state.value.routerList.forEach(item => {
          // console.log('item',item);
          if (item) item()
        })
       state.value = initState()
        // 删除本地缓存
        localStorage.removeItem("store")
      }
      return { state,selectMenu,undateTags,updateMenuList,addMenu,clean }
    }
    // ,
    // {
    //   persist: true
    // }
  )
  